﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TruongVu.Business;
using TruongVu.Data;

namespace TruongVuWebsite.Admin
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private ServiceModel _serviceModel = new ServiceModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                return;
            }

            if (Request["ID"] != null)
            {
                int id = -1;
                if(int.TryParse(Request["ID"], out id))
                {
                    Service service = this._serviceModel.Get(id);
                    if (service != null)
                    {
                        this.NameTextBox.Text = service.Name;
                        this.TimeTextBox.Text = service.Time;
                        this.CostTextBox.Text = service.Cost;
                        this.NationalCostTextBox.Text = service.NationalCost;
                        this.ContentEditor.Value = service.Details;
                        this.HighlightTextBox.Text = service.Title;
                    }
                }
            }
        }

        protected void SubmitButton_Click(object sender, ImageClickEventArgs e)
        {
            Service service = new Service();
            if (Request["ID"] != null)
            {
                int id = -1;
                if (int.TryParse(Request["ID"], out id))
                {
                    service.ID = id;
                }
            }

            service.Name = this.NameTextBox.Text;
            service.Time = this.TimeTextBox.Text;
            service.Cost = this.CostTextBox.Text;
            service.NationalCost = this.NationalCostTextBox.Text;
            service.Details = this.ContentEditor.Value;
            service.Title = this.HighlightTextBox.Text;

            if (Request["ID"] != null)
            {
                this._serviceModel.Update(service);
            }
            else
            {
                this._serviceModel.Insert(service);
            }

            Response.Redirect("ProductManagement.aspx");
        }
    }
}